| Conditions | 1 |
| Paths | 24 |
| Total Lines | 117 |
| Code Lines | 93 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 13 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | var TableHelper = { |
||
| 239 | function initInstance(){ |
||
| 240 | // Singleton |
||
| 241 | // Private methods and variables |
||
| 242 | var tail = null; |
||
| 243 | function Init(tableId){ |
||
| 244 | var tContainer = document.getElementById(tableId); |
||
| 245 | if(!iePrior(9)){ |
||
| 246 | TableHelper.Init.SetColumnsHoverEffect(tContainer, tableId); |
||
| 247 | } |
||
| 248 | var tfoot = tContainer.getElementsByTagName("tfoot")[0]; |
||
| 249 | TableHelper.ProcessPaginationLinks(tfoot); |
||
| 250 | } |
||
| 251 | function BuildRequest(rq, crntTableId, strDesc){ |
||
| 252 | rq.tableId = crntTableId; |
||
| 253 | TableHelper.BuildRequest.Sort(rq, strDesc); |
||
| 254 | TableHelper.BuildRequest.Filter(rq); |
||
| 255 | }; |
||
| 256 | function LoadData(tableContainer, rq){ |
||
| 257 | if(tail!==null){ tail.abort();} |
||
| 258 | TableHelperSetVisability(tableContainer, false); |
||
| 259 | var xmlhttp = window.XMLHttpRequest ? |
||
| 260 | new XMLHttpRequest() : /* code for IE7+, Firefox, Chrome, Opera, Safari */ |
||
| 261 | new window.ActiveXObject("Microsoft.XMLHTTP");/*code for IE6, IE5 */ |
||
| 262 | xmlhttp.onreadystatechange = function(){ |
||
| 263 | if(xmlhttp.readyState === 4 && xmlhttp.status === 200){ |
||
| 264 | Draw(tableContainer, JSON.parse(xmlhttp.responseText)); |
||
| 265 | TableHelperSetVisability(tableContainer, true); |
||
| 266 | instance.LoadEndCalback(tableContainer); |
||
| 267 | } |
||
| 268 | }; |
||
| 269 | xmlhttp.open("GET", RequestToUrl(rq), true); |
||
| 270 | xmlhttp.send(); |
||
| 271 | tail = xmlhttp; //put at tail to can abort later any previous |
||
| 272 | } |
||
| 273 | function Draw(tableContainer, d){ |
||
| 274 | TableHelperDraw_Section(tableContainer, d.body); |
||
| 275 | TableHelperDraw_Section(tableContainer, d.footer, "tfoot"); |
||
| 276 | if(instance.rq !== null){ |
||
| 277 | var hover = document.getElementById(instance.rq.tableId) |
||
| 278 | .getElementsByTagName("th")[instance.rq.colNo].lang; |
||
| 279 | if(hover){ |
||
| 280 | instance.ColumnHover(tableContainer, instance.rq.colNo); |
||
| 281 | } |
||
| 282 | } |
||
| 283 | } |
||
| 284 | |||
| 285 | var FilterGetTableId = TableHelperFilterGetTableId; |
||
| 286 | var GoPageGetNo = TableHelperGoPageGetNo; |
||
| 287 | var getParent = TableHelperGetParent; |
||
| 288 | var iePrior = TableHelperIePrior; |
||
| 289 | var RequestToUrl = TableHelperRequestToUrl; |
||
| 290 | |||
| 291 | return { |
||
| 292 | rq: null, |
||
| 293 | strAsc: String.fromCharCode(9650), //▲ |
||
| 294 | strDesc: String.fromCharCode(9660),//▼ |
||
| 295 | ReloadData: function(tableId){ |
||
| 296 | var request = {}; |
||
| 297 | BuildRequest(request, tableId, this.strDesc); |
||
| 298 | LoadData(tableId, request); |
||
| 299 | }, |
||
| 300 | Filter: function(field){ |
||
| 301 | var crntTableId = FilterGetTableId(field); |
||
| 302 | if(crntTableId !== null){ |
||
| 303 | var request = {}; |
||
| 304 | var exRq = this.rq; |
||
| 305 | BuildRequest(request, crntTableId, this.strDesc); |
||
| 306 | if(exRq === null || |
||
| 307 | request.filter !== exRq.filter || |
||
| 308 | request.filterBy !== exRq.filterBy |
||
| 309 | ){ |
||
| 310 | LoadData(crntTableId, request); |
||
| 311 | } |
||
| 312 | } |
||
| 313 | }, |
||
| 314 | GoPage: function(lnk){ |
||
| 315 | var request = {}; |
||
| 316 | var crntTableId = getParent(lnk, "table").getAttribute("id"); |
||
| 317 | BuildRequest(request, crntTableId, this.strDesc); |
||
| 318 | request.pageNo = GoPageGetNo(lnk, crntTableId); |
||
| 319 | LoadData(crntTableId, request); |
||
| 320 | return false; |
||
| 321 | }, |
||
| 322 | Export: function(lnk, eType){ |
||
| 323 | var request = {}; |
||
| 324 | var crntTableId = getParent(lnk, "table").getAttribute("id"); |
||
| 325 | BuildRequest(request, crntTableId, this.strDesc); |
||
| 326 | request.exportType = ["CSV", "Excel"].indexOf(eType) >= 0 ? |
||
| 327 | eType : |
||
| 328 | "csv"; |
||
| 329 | window.open(RequestToUrl(request)); |
||
| 330 | return false; |
||
| 331 | }, |
||
| 332 | Sort: function(colNo, lnk){ |
||
| 333 | var request = {}; |
||
| 334 | var crntTableId = getParent(lnk, "table").getAttribute("id"); |
||
| 335 | BuildRequest(request, crntTableId, this.strDesc); |
||
| 336 | if(Math.round(colNo) === request.colNo){ |
||
| 337 | request.colOrd = (request.colOrd === "asc" ? "desc" : "asc"); |
||
| 338 | }else{ |
||
| 339 | request.colNo = Math.round(colNo); |
||
| 340 | request.colOrd = "asc"; |
||
| 341 | } |
||
| 342 | LoadData(crntTableId, request); |
||
| 343 | /* Clear and add new sort arrow */ |
||
| 344 | var headSpans = getParent(lnk, "thead").getElementsByTagName("span"); |
||
| 345 | var length = headSpans.length; |
||
| 346 | for(var i = 0; i < length; i++){ |
||
| 347 | headSpans[i].innerHTML = ""; |
||
| 348 | } |
||
| 349 | lnk.getElementsByTagName("span")[0].innerHTML = (request.colOrd === "desc" ? this.strDesc : this.strAsc); |
||
| 350 | }, |
||
| 351 | ColumnHover: TableHelper.ColumnHover, //function(tableContainer, index) |
||
| 352 | LoadEndCalback: function(){},/*Allows override: function(tableId){if(tableId){...}}*/ |
||
| 353 | init: Init |
||
| 354 | }; |
||
| 355 | } |
||
| 356 | return { |
||
| 367 |
When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically: